package com.yunqiao.service.api.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunqiao.common.consts.AppConsts;
import com.yunqiao.common.validator.Assert;
import com.yunqiao.service.api.entity.UserEntity;
import com.yunqiao.service.api.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;

import com.yunqiao.service.api.mapper.ApplyDriverMapper;
import com.yunqiao.service.api.entity.ApplyDriverEntity;
import com.yunqiao.service.api.ApplyDriverService;
import org.springframework.transaction.annotation.Transactional;


@Service("applyDriverService")
public class ApplyDriverServiceImpl extends ServiceImpl<ApplyDriverMapper, ApplyDriverEntity> implements ApplyDriverService {

    @Autowired
    private ApplyDriverMapper applyDriverMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<ApplyDriverEntity> queryWrapper = new QueryWrapper<>();

        String status = MapUtil.getStr(params,"status");
        if(StrUtil.isNotBlank(status)){
            queryWrapper.eq("status",status);
        }


        queryWrapper.orderByDesc("create_time");
        IPage<ApplyDriverEntity> page = this.page(new Query<ApplyDriverEntity>(params).getPage(),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditApply(ApplyDriverEntity applyDriver) {
        ApplyDriverEntity dbApply = applyDriverMapper.selectById(applyDriver.getId());
        Assert.isNull(dbApply,"ID有误");

        dbApply.setStatus(applyDriver.getStatus());
        dbApply.setApplyReason(applyDriver.getApplyReason());
        applyDriverMapper.updateById(dbApply);

        UserEntity userEntity = userMapper.selectOne(new QueryWrapper<UserEntity>().eq("id", dbApply.getUserId()));
        userEntity.setApplyDriver(applyDriver.getStatus());

        if(applyDriver.getStatus().equals(AppConsts.USER_APPLY_DRIVER_2)){
            userEntity.setRoleType(AppConsts.USER_ROLE_TYPE_2);
            userEntity.setName(dbApply.getName());
            userEntity.setSex(dbApply.getSex());
            userEntity.setDriverLicense(dbApply.getDriverLicense());
            userEntity.setDrivingLicense(dbApply.getDrivingLicense());
            userEntity.setCarNumber(dbApply.getCarNumber());
            userEntity.setCarBrandColor(dbApply.getCarBrandColor());
            userEntity.setCarTypeId(dbApply.getCarTypeId());
            userEntity.setDrivingLicenseType(dbApply.getDrivingLicenseType());
            userEntity.setCompany(dbApply.getCompany());
            userEntity.setCarPic(dbApply.getCarPic());
            userEntity.setBusinessLicense(dbApply.getBusinessLicense());
            userEntity.setIdcardFront(dbApply.getIdcardFront());
            userEntity.setIdcardBack(dbApply.getIdcardBack());
            userEntity.setIdcardHandwith(dbApply.getIdcardHandwith());
            userEntity.setIdNumber(dbApply.getIdNumber());
            userEntity.setUrgencyContactName(dbApply.getUrgencyContactName());
            userEntity.setUrgencyContactPhone(dbApply.getUrgencyContactPhone());
        }
        userMapper.updateById(userEntity);
    }

}
